/* @(#)mutex.c
 * Time-stamp: <2008-10-20 00:29:16 root>
 * Copyright 2008 Julian Qian
 * Author: root@localhost.localdomain
 * Version: $Id: mutex.c,v 0.0 2008/10/07 18:31:59 root Exp $
 * Keywords: 
 */

#include "common.h"

mutex_t *mutex_create(int recursive){
	pthread_mutexattr_t attr;
	mutex_t *mutex;

	if(recursive){
		ASSERT(0 == pthread_mutexattr_setkind_np(&attr,PTHREAD_MUTEX_RECURSIVE_NP));
	}else{
		ASSERT(0 == pthread_mutexattr_setkind_np(&attr,PTHREAD_MUTEX_ERRORCHECK_NP));
	}
	mutex = (mutex_t*)malloc(sizeof(mutex_t));
	ASSERT(0 == pthread_mutex_init(mutex, &attr));
	return mutex;
}

void mutex_destroy(mutex_t *mutex){
	ASSERT(NULL != mutex);
	ASSERT(0 == pthread_mutex_destroy(mutex));
	free(mutex);
}

void mutex_lock(mutex_t *mutex){
	ASSERT(NULL != mutex);
	int ret;
	ret = pthread_mutex_lock(mutex);
	ASSERT(ret == 0);
}

void mutex_unlock(mutex_t *mutex){
	ASSERT(NULL != mutex);
	int ret;
	ret = pthread_mutex_unlock(mutex);
	ASSERT(ret == 0);
}
